<?php

class Ead_ModulosController extends App_Controller_Action
{
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        
        $search = $this->_getParam('inp-search', '');
        $this->view->search = $search;
        $find = 'MOD_ID > 0';
        if(strlen($search) > 1){
            $find = 'MOD_NOME like "%'.$search.'%"';
        }
        
        $obj = new Ead_Model_DbTable_Modulos();
        $result = $obj->fetchAll($find);
        $this->view->result = $this->paginacao($result);
         
    }
    
    
    public function editAction() {
        $form = new Ead_Form_Modulos();
        $form->submit->setLabel('Gravar');

        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('MOD_ID');
                $data = array(
                    'MOD_NOME'      => $form->getValue('MOD_NOME'),
                    'MOD_ARQUIVO'   => $form->getValue('MOD_ARQUIVO'),	
                    'MEN_ID'        => $form->getValue('MEN_ID'),	
                    'MOD_ICONE'     => $form->getValue('MOD_ICONE'),	
                    'MOD_ATIVO'     => $form->getValue('MOD_ATIVO'),
                    'MOD_HOME'      => $form->getValue('MOD_HOME'),
                    'MOD_MENU'      => $form->getValue('MOD_MENU'),
                    'MOD_ORDMEN'    => $form->getValue('MOD_ORDMEN'),
                    'MOD_ORDMENINI' => $form->getValue('MOD_ORDMENINI'),
                );
                $obj = new Ead_Model_DbTable_Modulos();

                if ($id == 0){
                    $id = $obj->insert($data);
                    $permisso = new Ead_Model_DbTable_PermissaoHasModulo();
                    $data_permissao = array(
                        'PER_ID' => 1,
                        'MOD_ID' => $id,
                    );
                    $permisso->insert($data_permissao);
                }
                else
                    $obj->update($data, 'MOD_ID = ' . $id);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Modulos();
                $form->populate($obj->fetchRow('MOD_ID = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Modulos();
            $obj->delete('MOD_ID = '.(int)$id);
            
            $permisso = new Ead_Model_DbTable_PermissaoHasModulo();
            $permisso->delete('MOD_ID = '.(int)$id);
            
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $obj = new Ead_Model_DbTable_Modulos();
            $form->populate($obj->fetchRow('MOD_ID = ' . $id)->toArray());
        }
    }

   



}

